﻿Public Class UploadManagerForm
    Inherits Form

    Friend WithEvents Client As Marmat.Common.IFileService
    Private m_date As Date = Date.Now
    Public Sub New(Client As Marmat.Common.IFileService)

        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me.Client = Client
    End Sub

    Friend ReadOnly Property AddCollection As List(Of FileManager.Item)
        Get
            If _AddCollection Is Nothing Then
                _AddCollection = New List(Of FileManager.Item)
            End If
            Return _AddCollection
        End Get
    End Property
    Private _AddCollection As List(Of FileManager.Item)


    Friend ReadOnly Property DelCollection As List(Of FileManager.Item)
        Get
            If _DelCollection Is Nothing Then
                _DelCollection = New List(Of FileManager.Item)
            End If
            Return _DelCollection
        End Get
    End Property
    Private _DelCollection As List(Of FileManager.Item)

    Private Sub FileManagerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each item As FileManager.Item In AddCollection
            Dim dr As Marmat.Forms.Views.DataGrid.Row = Me.BDataGrid1.AddNew
            dr("FileName") = item.Name
            dr("Size") = item.Size
            dr("Mode") = "上传"
            'todo 待修复
            'dr("State") = BI.ExternalLib.FileService.StateModeEnum.等待
            dr("Tag") = item
        Next
        For Each Item As FileManager.Item In DelCollection
            Dim dr As Marmat.Forms.Views.DataGrid.Row = Me.BDataGrid1.AddNew
            dr("FileName") = Item.Name
            dr("Mode") = "删除"

            'todo 待修复
            'dr("State") = BI.ExternalLib.FileService.StateModeEnum.等待
            dr("Tag") = Item
        Next
        Me.BDataGrid1.AcceptChanges()

        'todo 暂时注释，等待恢复
        'Client_UploadFileCompleted(Client, Nothing)
    End Sub

    Private Row As Marmat.Forms.Views.DataGrid.Row


    'todo 暂时注释，等待恢复
    '''' <summary>
    '''' 异步上传进度发生改变触发的事件
    '''' </summary>
    '''' <param name="sender"></param>
    '''' <param name="e"></param>
    '''' <remarks></remarks>
    'Private Sub IFileService_UploadProgressChanged(sender As Object, e As ExternalLib.FileService.UploadProgressChangedEventArgs) Handles Client.UploadProgressChanged
    '    Dim dr As  Marmat.Forms.Views.DataGrid.Row = Me.Row
    '    If dr IsNot Nothing Then
    '        dr("Posstion") = e.BytesSent
    '        Dim ProgressPercentage As Decimal = (e.BytesSent / e.TotalBytesToSend) * 100
    '        dr("Progress") = ProgressPercentage ' e.ProgressPercentage
    '    End If
    'End Sub

    'todo 暂时注释，等待恢复
    '''' <summary>
    '''' 异步上传文件完成之后触发的事件
    '''' </summary>
    '''' <param name="sender"></param>
    '''' <param name="e"></param>
    '''' <remarks></remarks>
    'Private Sub Client_UploadFileCompleted(sender As Object, e As ExternalLib.FileService.UploadFileCompletedEventArgs) Handles Client.UploadFileCompleted
    '    If Me.Row IsNot Nothing Then
    '        Dim dr As  Marmat.Forms.Views.DataGrid.Row = Me.Row
    '        If e IsNot Nothing AndAlso dr IsNot Nothing AndAlso dr("Mode") = "上传" Then
    '            If e.Cancelled = False AndAlso e.Error Is Nothing Then
    '                dr("Progress") = 100
    '                dr("Posstion") = dr("Size")
    '                dr("State") = StateModeEnum.成功
    '            Else
    '                dr.BaseRow.SetColumnError("State", e.Error.ToString)
    '                dr("State") = StateModeEnum.失败
    '            End If
    '        End If
    '    End If
    '    For Each r As  Marmat.Forms.Views.DataGrid.Row In Me.BDataGrid1.Rows
    '        If r("State") = StateModeEnum.等待 Then
    '            If r("Mode") = "上传" Then
    '                Row = r
    '                r("State") = StateModeEnum.执行
    '                Dim Value As FileManager.ValueCollection.Value = r("Tag")
    '                Client.UploadAsync(Value.LocalPath, Value.Path)
    '                Exit For
    '            ElseIf r("Mode") = "删除" Then
    '                r("State") = StateModeEnum.执行
    '                Dim Value As FileManager.ValueCollection.Value = r("Tag")
    '                Try
    '                    Client.Delete(Value.Path)
    '                    r("Progress") = 100
    '                    r("State") = StateModeEnum.成功
    '                Catch ex As Exception
    '                    r("State") = StateModeEnum.失败
    '                End Try
    '                Client_UploadFileCompleted(Client, Nothing)
    '                Exit For
    '            End If
    '        End If
    '    Next
    'End Sub

    Private Sub BDataGrid1_CustomDrawCell(sender As Object, e As Marmat.Forms.Views.DataGrid.RowCellCustomDrawEventArgs) Handles BDataGrid1.CustomDrawCell
        Select Case e.Column.Name
            Case "Size"
                If e.CellValue Is DBNull.Value Then Return
                Dim Value As Long = e.CellValue
                If Value >= 1024 ^ 4 Then
                    e.DisplayText = Format(Value / 1024 ^ 4, "f") & " TB"
                ElseIf Value >= 1024 ^ 3 Then
                    e.DisplayText = Format(Value / 1024 ^ 3, "f") & " GB"
                ElseIf Value >= 1024 ^ 2 Then
                    e.DisplayText = Format(Value / 1024 ^ 2, "f") & " MB"
                Else
                    e.DisplayText = Format(Value / 1024 ^ 1, "f") & " KB"
                End If
            Case "Progress"
                If e.CellValue Is DBNull.Value Then
                    e.DisplayText = "0%"
                Else
                    Dim x As Integer = (e.Bounds.Width - 4) * CSng(e.CellValue / 100)
                    Using GradientBrush As New System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(122, 197, 255), Color.FromArgb(100, 122, 197, 255), 90)
                        e.Graphics.FillRectangle(GradientBrush, e.Bounds.X + 2, e.Bounds.Y + 2, x, e.Bounds.Height - 6)
                        e.Graphics.DrawRectangle(New Pen(Color.FromArgb(100, 197, 255), 1), e.Bounds.X + 2, e.Bounds.Y + 2, x, e.Bounds.Height - 6)
                    End Using
                    e.DisplayText = Format(e.CellValue / 100, "Percent")
                End If
        End Select
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Me.IsDisposed = False Then
            Dim SumPosstion As Long = 0
            Dim SumSize As Long = 0
            For Each dr As Marmat.Forms.Views.DataGrid.Row In Me.BDataGrid1.Rows
                If dr("Mode") = "上传" Then
                    SumSize += dr("Size")
                    SumPosstion += dr("Posstion")
                End If
            Next
            Dim jd As Decimal = SumPosstion / SumSize
            Dim txt As String = IIf(SumSize > 0, Format(jd, "Percent"), "0%")
            If txt = "100.00%" Then txt = "100%"
            Me.Label1.Text = "当前进度：" & txt

            Dim t As TimeSpan = Date.Now - m_date
            Me.Label2.Text = "传输用时：" & IIf(t.Days > 0, t.Days & "天", Nothing) & IIf(t.Hours > 0, t.Hours & "时", Nothing) & IIf(t.Minutes > 0, t.Minutes & "分", Nothing) & IIf(t.Seconds > 0, t.Seconds & "秒", Nothing)
            If Me.Label2.Text = "传输用时：" Then
                Me.Label2.Text = "传输用时：0秒"
            End If
            If SumPosstion > 0 Then
                'Dim f As Integer = (1 - jd) * (t.TotalSeconds / jd)
                Dim mValue As Integer = CInt(t.TotalSeconds / jd - t.TotalSeconds)
                t = New TimeSpan(0, 0, mValue)
                Dim msg As String = IIf(t.Days > 0, t.Days & "天", Nothing) & IIf(t.Hours > 0, t.Hours & "时", Nothing) & IIf(t.Minutes > 0, t.Minutes & "分", Nothing) & IIf(t.Seconds > 0, t.Seconds & "秒", Nothing)
                Me.Label3.Text = "预计剩余时间：" & IIf(msg > "", msg, "0秒")
            End If

            For Each dr As Marmat.Forms.Views.DataGrid.Row In Me.BDataGrid1.Rows
                If dr("State") = StateModeEnum.等待 Or dr("State") = StateModeEnum.执行 Then
                    Exit Sub
                End If
            Next
            Me.Timer1.Enabled = False
            Me.Label3.Text = "预计剩余时间：0秒"
            ' Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub


End Class